package pers.li.service;

import pers.li.dao.CategoryDao;
import pers.li.dao.GoodsDao;
import pers.li.dao.GoodsDetailDao;
import pers.li.model.*;
import pers.li.model.VO.GoodsVO;
import pers.li.utils.BeanCopy;

import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class GoodsService {
    private final GoodsDao goodsDao = new GoodsDao();
    private final CategoryDao categoryDao = new CategoryDao();
    private final GoodsDetailDao goodsDetailDao = new GoodsDetailDao();


    public List<Goods> getToday() {
        List<Goods> today = goodsDao.getToday();
        return today.stream().limit(5).collect(Collectors.toList());
    }

    public List<Goods> getHotGoods(int num) {
        return goodsDao.getHotGoods(num);
    }

    public List<Goods> getNewGoods(int num) {
        return goodsDao.getNewGoods(num);
    }

    /**
     * 根据foodId获取商品详情页的商品id
     *
     * @param goodId 商品
     * @return 商品详情VO对象
     */
    public GoodsVO getGoodsVO(Integer goodId) {
        GoodsVO goodsVO = new GoodsVO();
        Goods good = goodsDao.getGoodsByGoodId(goodId);
        Category category = categoryDao.getCategoryByCategoryId(good.getCateId());
        List<GoodsDetailType> goodsDetailType = goodsDetailDao.getGoodsDetailTypeByGoodId(goodId);
        Map<Integer, List<GoodsDetail>> goodsDetailMap = new HashMap<>();
        if (goodsDetailType != null) {
            for (GoodsDetailType detailType : goodsDetailType) {
                Integer typeId = detailType.getTypeId();
                List<GoodsDetail> goodsDetail = goodsDetailDao.getGoodsDetailByGoodIdAndDetailTypeId(goodId, typeId);
                goodsDetailMap.put(typeId, goodsDetail);
            }
        }
        List<Pic> picList = goodsDao.getPicByGoodId(goodId);
        BeanCopy.copy(good, goodsVO);
        goodsVO.setCateName(category.getCateName());
        goodsVO.setGoodsDetailTypeList(goodsDetailType);
        goodsVO.setGoodsDetailMap(goodsDetailMap);
        goodsVO.setPicList(picList);
        return goodsVO;
    }

    public List<Goods> getGoodsByKeyword(String keyWord) {
        return goodsDao.getGoodsByKeyword(keyWord);
    }

    public Goods getGoodsByGoodId(Integer goodId) {
        return goodsDao.getGoodsByGoodId(goodId);
    }

    public List<Goods> getGoodsByCateId(Integer cateId) {
        return goodsDao.getGoodsByCateId(cateId);
    }

    public List<Goods> getGoodsByCateIdSort(Integer cateId, String sort) {
        switch (sort) {
            case "1":
                return goodsDao.getGoodsByCateIdSort(cateId, Comparator.comparingDouble(Goods::getGoodsDiscount));
            case "2":
                return goodsDao.getGoodsByCateIdSort(cateId, Comparator.comparingDouble(Goods::getGoodsDiscount).reversed());
            case "3":
                return goodsDao.getGoodsByCateIdSort(cateId, Comparator.comparingInt(Goods::getGoodsSales).reversed());
            case "4":
                return goodsDao.getGoodsByCateIdSort(cateId, Comparator.comparingInt(Goods::getGoodsSales));
        }
        return null;
    }

    public String getContentName(Integer contentId){
        return goodsDetailDao.getContentName(contentId);
    }

}
